package cn.wk.pet.service.impl;

import cn.wk.basic.domain.Point;
import cn.wk.basic.service.impl.BaseServiceImpl;
import cn.wk.basic.utils.DistanceUtil;
import cn.wk.org.domain.Shop;
import cn.wk.org.mapper.ShopMapper;
import cn.wk.pet.domain.SearchMasterMsg;
import cn.wk.pet.mapper.SearchMasterMsgMapper;
import cn.wk.pet.service.ISearchMasterMsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    /**
     * 保存寻主信息
     * @param searchMasterMsg
     */
    @Override
    @Transactional
    public void publish(SearchMasterMsg searchMasterMsg) {
        //根据用户输入的地址获取经纬度
        Point point = DistanceUtil.getPoint(searchMasterMsg.getAddress());
        //然后根据地址去寻找最近的门店
        //先要找到所有的门店
        List<Shop> all = shopMapper.findAll();
        //得到最近的一家门店
        Shop nearestShop = DistanceUtil.getNearestShop(point, all);
        //把其添加进去
        searchMasterMsg.setShop(nearestShop);
        //保存
        searchMasterMsgMapper.save(searchMasterMsg);

    }
}
